// 2013-7-29上午12:06:06  net.betou.trade.admin.action.ProfileAct.java

package net.betou.trade.admin.action;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.util.TradeUtils;
import net.betou.common.web.springmvc.RealPathResolver;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.AgentAndAdminsExt;
import net.betou.trade.entity.main.Picture;
import net.betou.trade.main.manager.PictureMng;
import net.betou.trade.main.manager.agencyorproxy.AdminMng;
import net.betou.trade.main.manager.agencyorproxy.AgencyMng;
import net.betou.trade.main.manager.agencyorproxy.SystemAdminMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.ResponseUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 个人信息设置
 * @author 叶装装
 */
@Controller
public class ProfileAct {
	
	/**
	 * 因为系统的Admin 访问时都会在更新， 所以这里直接取
	 * @return
	 */
	@RequestMapping( "/profile/v_edit.jspx")
	public String toprofile( HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		String forward;
		if( admin.getSystem( ) ){
			forward = "profile/system";
		}else if( !admin.getSystem( )  && !admin.getJustAdmin( ) ){   // 居间商
			forward = "profile/agent";
		}else{
			forward = "profile/agentadmin";
		}
		model.addAttribute( "bean", admin );
		return forward;
	}
	
	@RequestMapping ( "/profile/o_update_systemadmin.jspx" )
	public String o_update( Boolean disabled,  Integer id, String password,  String email,  Integer[ ] roleIds, AgentAndAdminsExt ext, HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		AgentAndAdmins bean = systemAdminMng.findById( admin.getId( ) ) ;
		
		bean = systemAdminMng.update( disabled, password, email, bean, ext, roleIds );
		
		return "redirect:v_edit.jspx";
	}
	
	@RequestMapping ( "/profile/o_update_agent.jspx" )
	public String o_update( Boolean disabled, Integer id, String password, String mobile, String intermediaName, String email, Integer [ ] roleIds,  String [] pic, Integer[] picIds, AgentAndAdminsExt ext, HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		AgentAndAdmins bean = agencyMng.findAgency( admin.getId( ) );
		
		if( picIds != null ){
			picMng.deleteByIds( picIds );
		}
		
		if( pic != null ){
			for( String p : pic ){
				 picMng.save( bean, p, null );
			}
		}
		
		bean = agencyMng.update( disabled, password, email, mobile, intermediaName, bean, ext, roleIds, pic, picIds );
		
		return "redirect:v_edit.jspx";
	}
	
	@RequestMapping ( "/profile/o_update_agentadmin.jspx" )
	public String o_update( Boolean disabled,  String password,  String email, String mobile,  Integer[ ] roleIds, AgentAndAdminsExt ext, HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		AgentAndAdmins bean = adminMng.findById( admin.getId( )  );
		
		// 更新居间管理员
		bean = adminMng.update( disabled, password, email, mobile, bean, ext, roleIds );
		
		return "redirect:v_edit.jspx";
	}
	
	@RequestMapping( "/profile/o_delete_img.jspx" )
	public void o_delete( Integer id, HttpServletResponse response, HttpServletRequest request ){
		
		
		ResponseUtil util = ResponseUtil.create( response );

		Picture p = picMng.findById( id );
		
		if( p == null ){
			util.addErrorResponse( Messages.DATA_NOT_EXIST );
			util.renderJson( );
			return;
		}
		
		picMng.deleteById( id );
		
		String path = p.getPath( );
		String deletePath = realPath.get( path );
		
		File file = new File( deletePath );
		
		file.delete( );
		
		util.addSuccessResponse( Messages.DATA_DELETE_SUCCESS );

		util.renderJson( );
		return;
	}
	
	@Autowired
	private AdminMng adminMng;
	
	@Autowired
	private SystemAdminMng systemAdminMng;
	
	@Autowired
	private AgencyMng agencyMng;
	
	@Autowired
	private PictureMng picMng;
	
	@Autowired
	private RealPathResolver realPath;
	
}
